package com.fwzc.giuseppe.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.adapter.UniversalAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.entity.Goods;
import com.fwzc.giuseppe.util.ViewHolder;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * 收藏夹
 * 
 * @author llf
 * 
 */
@SuppressLint("InflateParams") 
public class FavoriteActivity extends BaseActivity implements OnClickListener {
	private GridView favorite;
	private ArrayList<Goods> datas = new ArrayList<Goods>();
	private ProgressDialog load;
	private HttpUtils httpUtil;
	private BitmapUtils bitmapUtil;
	private String userId;
	private UniversalAdapter<Goods> adapter;
	private Dialog delete_dialog;
	private RelativeLayout delete_view;
	private Button delete;
	private String favoritelid;
	private int positionId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favorite);

		initBar();
		initView();
		initClick();
	}

	private void initView() {
		// 网络请求相关
		httpUtil = new HttpUtils();
		bitmapUtil = new BitmapUtils(this);
		load = new ProgressDialog(this);
		load.setMessage(getResources().getString(R.string.deal));
		load.setCancelable(false);
		userId = getSharedPreferences("user", Activity.MODE_PRIVATE).getString(
				"userId", "");
		
		delete_view = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.dialog_delete, null);
		delete = (Button)delete_view.findViewById(R.id.delete);
		delete_dialog = new Dialog(this);
		delete_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		delete_dialog.setContentView(delete_view);
		
		actionbar_side_name.setText("收藏夹");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		bg_actionbar_side.setBackgroundColor(getResources().getColor(
				R.color.transparent));
		iv_actionbar_side_left.setOnClickListener(this);

		favorite = (GridView) findViewById(R.id.favorite);
		// 伪数据
//		for (int i = 0; i < 6; i++) {
//			Goods goods = new Goods();
//			datas.add(goods);
//		}
		// 伪数据结束

		adapter = new UniversalAdapter<Goods>(this, datas,
				R.layout.clothes_item) {

			@Override
			public void convert(ViewHolder viewHolder, Goods goods) {
				ImageView image = (ImageView) viewHolder.getView(R.id.image);
				TextView name = (TextView) viewHolder.getView(R.id.name);
				TextView number = (TextView) viewHolder.getView(R.id.number);
				TextView price = (TextView) viewHolder.getView(R.id.price);

				name.setText(goods.goods_name);
				number.setText("已售" + goods.number + "件");
				price.setText(goods.price + "元");
				BitmapDisplayConfig config = new BitmapDisplayConfig();
				config.setLoadFailedDrawable(getResources().getDrawable(R.drawable.error));
				bitmapUtil.display(image, goods.goods_url,config);
			}
		};
		favorite.setAdapter(adapter);
		//快速滑动时停止图片的加载
		favorite.setOnScrollListener(new PauseOnScrollListener(bitmapUtil,false, true));
		showFavorite();
	}

	private void showFavorite() {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("userId", userId);
		params.addBodyParameter("type", "0");
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.FAVORITE, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 4) {
								JSONArray ja = response.getJSONArray("dataList");
								for (int i = 0; i < ja.length(); i++) {
									JSONObject jo = ja.getJSONObject(i);
									Goods goods = new Goods();
									goods.goods_id = jo.getInt("suitid");
									goods.goods_name = jo.getString("name");
									goods.price = jo.getString("price");
									goods.number = jo.getString("amount");
									goods.goods_url = jo.getString("imgurl");
									goods.favoritelid = jo.getString("favoritelid");
									datas.add(goods);
								}
								adapter.notifyDataSetChanged();
								load.dismiss();
							}else{
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}

					}
				});
	}

	private void delete(String favoritelid) {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("userId", userId);
		params.addBodyParameter("type", "2");
		params.addBodyParameter("favoriteLId", favoritelid);
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.FAVORITE, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("删除失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 8) {
								datas.remove(positionId);
								adapter.notifyDataSetChanged();
								load.dismiss();
							}else{
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}
					}
				});
	}

	private void initClick() {
		favorite.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(FavoriteActivity.this,CommodityDetails.class);
				// intent.putExtra("suitId", datas.get(position).goods_id);
				startActivity(intent);
			}
		});

		favorite.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				favoritelid = datas.get(position).favoritelid;
				positionId = position;
				delete_dialog.show();
				return true;
			}
		});
		
		delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				delete_dialog.dismiss();
				delete(favoritelid);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_actionbar_side_left:
			finish();
			break;

		default:
			break;
		}
	}
}
